﻿Imports System.Data.OleDb
Imports System.Xml
Imports System.ServiceModel.Syndication


Public Class rss
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.ContentType = "application/rss+xml"

        Dim myFeed As New SyndicationFeed

        myFeed.Title = TextSyndicationContent.CreatePlaintextContent("WalkingWeb Routes")
        myFeed.Description = TextSyndicationContent.CreatePlaintextContent("The most recent routes added to WalkingWeb")
        myFeed.Links.Add(SyndicationLink.CreateSelfLink(New Uri(okURL("list.aspx"))))
        myFeed.Language = "en-us"

        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("walkingweb").ConnectionString)
        oleDbConn.Open()
        Dim cmdQuery As String = "SELECT TOP 5 * from routes"
        Dim dbcomm = New OleDbCommand(cmdQuery, oleDbConn)
        Dim dbread = dbcomm.ExecuteReader()

        Dim MyItems As New List(Of SyndicationItem)
        While dbread.Read()
            Dim sItem As New SyndicationItem
            sItem.Title = mkRssTxt(dbread("RouteName"))
            sItem.Summary = mkRssTxt(dbread("Description") & dbread("City"))
            MyItems.Add(sItem)
        End While

        myFeed.Items = MyItems

    End Sub

    Private Function mkRssTxt(ByVal someString As String) As TextSyndicationContent
        Return TextSyndicationContent.CreatePlaintextContent(someString)
    End Function


    Private Function okURL(ByVal urlStr As String)
        Return Request.Url.GetLeftPart(UriPartial.Authority) & ResolveUrl(urlStr)
    End Function

    Protected Function FormatForXML(input As Object) As String
        Dim data As String = input.ToString()
        ' cast the input to a string
        ' replace those characters disallowed in XML documents
        data = data.Replace("&", "&amp;")
        data = data.Replace("""", "&quot;")
        data = data.Replace("'", "&apos;")
        data = data.Replace("<", "&lt;")
        data = data.Replace(">", "&gt;")

        Return data
    End Function

End Class


